﻿/*
 * zarray.cxx
 *
 *  Created on: 2015年4月3日
 *      Author: Fifi Lyu
 */

#include "zpsoe/zalgorithm/zarray.h"
#include "zpsoe/zalgorithm/zint.h"
#include "zpsoe/zalgorithm/zstring.h"
#include <algorithm>

using std::sort;

namespace zpsoe {

namespace zalgorithm {

namespace zarray {

ZPSOE_SHARED_LIB_API bool find(const vector<string> &array, const string &element) {
    return (find(array.begin(), array.end(), element) != array.end());
}

ZPSOE_SHARED_LIB_API bool find(const vector<uint16_t> &array, const uint16_t &element) {
    return (find(array.begin(), array.end(), element) != array.end());
}

ZPSOE_SHARED_LIB_API bool find(const vector<uint32_t> &array, const uint32_t &element) {
    return (find(array.begin(), array.end(), element) != array.end());
}

ZPSOE_SHARED_LIB_API bool find(const vector<uint64_t> &array, const uint64_t &element) {
    return (find(array.begin(), array.end(), element) != array.end());
}

ZPSOE_SHARED_LIB_API bool find(const vector<double> &array, const double &element) {
    return (find(array.begin(), array.end(), element) != array.end());
}

ZPSOE_SHARED_LIB_API bool find(
        vector<string> &array, const string &element, vector<string>::iterator &it) {
    for (vector<string>::iterator it_ = array.begin(); it_ != array.end(); ++it_) {
        if ((*it_) == element) {
            it = it_;
            return true;
        }
    }

    return false;
}

ZPSOE_SHARED_LIB_API bool find(
        list<string> &array, const string &element, list<string>::iterator &it) {
    list<string>::iterator it_;
    for (it_ = array.begin(); it_ != array.end(); it_++) {
        if ((*it_) == element) {
            it = it_;
            return true;
        }
    }

    return false;
}

ZPSOE_SHARED_LIB_API bool exist(vector<string> &array, const string &element) {
    vector<string>::iterator it;
    return find(array, element, it);
}

ZPSOE_SHARED_LIB_API bool exist(list<string> &array, const string &element) {
    list<string>::iterator it;
    return find(array, element, it);
}

ZPSOE_SHARED_LIB_API void unique(vector<string> &array) {
    if (array.empty())
        return;

    // 去重之前必须排序
    sort(array.begin(), array.end());

    // 获取重复元素位置
    vector<string>::iterator it_ = unique(array.begin(), array.end());

    if (it_ != array.end())
        array.erase(it_, array.end());
}

ZPSOE_SHARED_LIB_API void unique(vector<int64_t> &array) {
    if (array.empty())
        return;

    // 去重之前必须排序
    sort(array.begin(), array.end());

    // 获取重复元素位置
    vector<int64_t>::iterator it_ = unique(array.begin(), array.end());

    if (it_ != array.end())
        array.erase(it_, array.end());
}

} /* namespace zarray */

} /* namespace zalgorithm */

} /* namespace zpsoe */
